package com.jack.payment.service.strategy.impl;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;

import com.alipay.api.DefaultAlipayClient;
import com.jack.payment.cache.CacheRedisServiceImpl;
import com.jack.payment.constant.enums.PayClientEnum;
import com.jack.payment.constant.SettingRuleConstant;
import com.jack.payment.dao.config.ConfigPayAliDao;
import com.jack.payment.entity.business.ApplyRefundBo;
import com.jack.payment.entity.business.RequestPayBo;
import com.jack.payment.entity.common.BusinessException;
import com.jack.payment.entity.generate.config.ConfigPayAliModel;
import com.jack.payment.entity.result.ResultPayVo;
import com.jack.payment.service.alipay.impl.AliPaymentMethodServiceImpl;
import com.jack.payment.service.strategy.PaymentStrategy;
import com.jack.payment.util.auth.DataAuthUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("AliPay")
public class AliPaymentService implements PaymentStrategy {
    private Logger log = LoggerFactory.getLogger(AliPaymentService.class);

    private AlipayClient alipayClient;
    private Integer payUseModule;   // 支付宝使用的支付模式 1:密钥 2:证书

    @Autowired
    private ConfigPayAliDao configPayAliDao;
    @Autowired
    private DataAuthUtils dataAuthUtils;
    @Autowired
    private AliPaymentMethodServiceImpl aliPaymentMethodService;
    @Autowired
    private CacheRedisServiceImpl cacheRedisService;


    @Override
    public synchronized void updatePaymentMethodConfig(Integer id) throws BusinessException {
        ConfigPayAliModel payAliModel = dataAuthUtils.decodeConfigAliPay(configPayAliDao.getAppointConfigPayAli(id));
        payUseModule = payAliModel.getType();
        try {
            alipayClient = new DefaultAlipayClient(aliPaymentMethodService.setAliPaymentClientConfig(payAliModel));
        } catch (AlipayApiException e) {
            log.error("严重配置错误!!! 这可能导致支付宝支付服务不可用, 错误信息如下" + e);
            e.printStackTrace();
        }
    }

    @Override
    public ResultPayVo createPaymentInfo(RequestPayBo requestPayBo) throws Exception {
        ResultPayVo resultPayVo = new ResultPayVo();
        PayClientEnum payClientEnum = PayClientEnum.matchingPlatform(requestPayBo.getPayPlatform());
        String webHook = cacheRedisService.getCacheRuleContent(SettingRuleConstant.ALIPAY_WEBHOOK);
        switch (payClientEnum) {
            // iOS , Android 统一为 app 支付
            case APP ->
                    resultPayVo = aliPaymentMethodService.createAliAppPayInfo(
                            requestPayBo,
                            webHook,
                            payUseModule,
                            alipayClient
                    );
            // Web 进行扫码
            case WEB ->
                    resultPayVo = aliPaymentMethodService.createAliQrcodePayInfo(
                            requestPayBo,
                        webHook,
                        payUseModule,
                        alipayClient
                    );
            // 小程序 或 H5
            case APPLET ->
                    resultPayVo = aliPaymentMethodService.createAliWebPayInfo(
                            requestPayBo,
                            webHook,
                            payUseModule,
                            alipayClient
                    );
            default -> log.warn("未能找到合适的支付平台信息{}", payClientEnum.getValues());
        }
        return resultPayVo;
    }

}
